const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
// 引入路由 --> 【做了自动化引入并注册】
// const userRouter = require("../router/user.router");
// const loginRouter = require("../router/login.router");
const registerRouters = require("../router");
const { createCorsMiddleware } = require("./cors");
const { createStaticMiddleware } = require("./static");
const { createIpMiddleware } = require("./ip");
const createWebSocket = require("../websocket/websocket"); // 引入 WebSocket 模块

// 1.创建app
const app = new Koa();

// 获取客户端ip
app.use(createIpMiddleware());

// 配置cors跨域资共享
app.use(createCorsMiddleware());

// 创建静态资源服务器
app.use(createStaticMiddleware());

// 2.对app使用中间件
app.use(bodyParser());

// app.use(userRouter.routes());
// app.use(userRouter.allowedMethods());
// app.use(loginRouter.routes());
// app.use(loginRouter.allowedMethods());

// 自动化注册路由
registerRouters(app);

// 创建 WebSocket
createWebSocket(app);

// 3.导出app
module.exports = app;
